﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SBACN.WebSite.Code;
using System.Text;
using SBACN.WebSite.Cache;
using SBACN.WebSite.Common;

namespace SBACN.WebSite.UC
{
	public partial class Header : BaseControl
	{
		/// <summary>
		/// 搜索页的Url
		/// </summary>
		public string SearchPageUrl
		{
			get
			{
				if (ViewState["SearchPageUrl"] == null || string.IsNullOrWhiteSpace(ViewState["SearchPageUrl"].ToString()))
				{
					return "/Search.aspx";
				}
				else
				{
					return ViewState["SearchPageUrl"].ToString();
				}
			}
			set
			{
				ViewState["SearchPageUrl"] = value;
			}
		}

		public string HeaderMenuHtml
		{
			get;set;
		}

		/// <summary>
		/// 网站地图Url
		/// </summary>
		public string WebMapUrl
		{
			get
			{
				if (ViewState["WebMapUrl"] == null || string.IsNullOrWhiteSpace(ViewState["WebMapUrl"].ToString()))
				{
					return "/WebMap.aspx";
				}
				else
				{
					return ViewState["WebMapUrl"].ToString();
				}
			}
			set
			{
				ViewState["WebMapUrl"] = value;
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				InitCtrl();
			}
		}

		public void InitCtrl()
		{
			StringBuilder sb = new StringBuilder();
			sb.AppendLine("<ul>");
			sb.AppendLine("<li " + (Request.Path.ToLower().Trim().Trim('/') == "index.aspx" || string.IsNullOrWhiteSpace(SystemCode) ? " class=\"a\" " : "") + "><a onmouseover=\"ShowMenu(this)\" onmouseout=\"HideMenu(this)\" href=\"/Index.aspx\">首页</a></li>");
			var headerList = SystemMapCache.Instance.List.Where(p => p.Code.Length == 4 && p.ShowInMap && !string.IsNullOrWhiteSpace(p.CURL)).ToList();

			StringBuilder sbheader = new StringBuilder();
			foreach(var item in headerList)
			{
				sb.AppendLine("<li " + ((item.Code == MenuCode) ? "class=\"a\" " : " id='mn_portal_" + item.Code + "' onmouseover=\"navShow('portal_" + item.Code + "',this)\" ") + "><a href=\"" + BasePage.UrlAppendQueryString(item.CURL, "st", item.Code, false) + "\">" + item.CText + "</a></li>");
				//if(item.Code != MenuCode)
				{
					sbheader.AppendLine("<ul style=\"display: none;\" id=\"snav_mn_portal_" + item.Code + "\" class=\"cl\">");
					var childMenu = SystemMapCache.Instance.List.Where(p => p.Code.StartsWith(item.Code) && p.Code.Length == 8 && p.ShowInMap).ToList();
					foreach(var child in childMenu)
					{
						string menuUrl = string.Empty;
						if(string.IsNullOrWhiteSpace(child.CURL))
						{
							var grandChild = SystemMapCache.Instance.List.Where(p => p.Code.StartsWith(child.Code) && p.Code.Length == 12 && p.ShowInMap).OrderBy(p => p.SortIndex).First();
							menuUrl = "href='" + BasePage.UrlAppendQueryString(grandChild.CURL, "st", grandChild.Code, false) + "'" + (grandChild.OpenInNewWindow ? " target='blank' " : string.Empty);
						}
						else
							menuUrl = "href='" + BasePage.UrlAppendQueryString(child.CURL, "st", child.Code, false) + "'" + (child.OpenInNewWindow ? " target='blank' " : string.Empty);

						//sbheader.AppendLine("<li><a hidefocus=\"true\" " + menuUrl + " title='" + child.CText + "' >" + child.CText.CutTo(8, 7) + "</a></li>"); 

						//增加菜单右侧的锁标志
						if (child.NeedMemberLogin) //会员服务版块
						{
							sbheader.AppendLine("<li><a hidefocus=\"true\" " + menuUrl + " title='" + child.CText + "' ><span>" + child.CText + "<img src=\"/images/lock_sm.png\" height=\"12px\" width=\"12px\" /></span></a></li>");
							
						}
						else //非会员服务版块
						{
							sbheader.AppendLine("<li><a hidefocus=\"true\" " + menuUrl + " title='" + child.CText + "' ><span>" + child.CText + "</span></a></li>");
						}
					}
					sbheader.AppendLine("</ul>");
				}
			}
			sb.AppendLine("</ul>");
			this.nv.InnerHtml = sb.ToString();
			this.HeaderMenuHtml = sbheader.ToString();
			if(MemberBasePage.UserInfo != null)
			{
				this.lbl_LogInfo.Text = "<a>" + (string.IsNullOrWhiteSpace(MemberBasePage.UserInfo.NickName) ? MemberBasePage.UserInfo.UserName : MemberBasePage.UserInfo.NickName) + "</a>&nbsp;|&nbsp;<a href='javascript:void(0);' onclick='return DoLogout();'>注销</a>&nbsp;";
			}

			/*
			 
	<ul style="display: none;" id="snav_mn_portal" class="cl ">
		<li><a hidefocus="true" href="#">公司业务专委会</a></li>
		<li><a hidefocus="true" href="#">个人业务专委会</a></li>
		<li><a hidefocus="true" href="#">银团贷款专委会</a></li>
		<li><a hidefocus="true" href="#">银行卡专委会</a></li>
		<li><a hidefocus="true" href="#">创新发展专委会</a></li>
		<li><a hidefocus="true" href="#">外资银行专委会</a></li>
		<li><a hidefocus="true" href="#">金融消费者权益保护专委会</a></li>
		<li><a hidefocus="true" href="#">风险管理专委会</a></li>
		<li><a hidefocus="true" href="#">合规管理专委会</a></li>
	</ul>
			 */
		}

		protected void btn_Logout_Click(object sender, EventArgs e)
		{
			//if (MemberBasePage.UserInfo != null)
			{
				Session.Clear();
				Session.Abandon();
				Response.Redirect(BasePage.MemberDefaultPage, true);
			}
		}
	}
}